package feedback;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.util.HttpUtils;
import com.util.Info;
import com.zhny.wxginger.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import vo.Lyzx;

public class FeedbackRecordActivity extends ListActivity {

	private ListView mListView = null;

	private ListAdapter mAdapter = null;

	private ImageView mRightBtn = null;
	
	private TextView mTitle = null;
	
	private List<Lyzx> list=new ArrayList<Lyzx>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.my_feedback);
		initTitle();
		new queryQues().execute(Info.path5);
	}

	private void initTitle() {
		mListView = getListView();
		mRightBtn = (ImageView) findViewById(R.id.right_btn);
		mRightBtn.setImageResource(R.drawable.icon_feedback);
		mRightBtn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				FeedbackRecordActivity.this.finish();
			}
		});
		
		mTitle = (TextView) findViewById(R.id.title_name);
		mTitle.setText(R.string.feedback_record);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
			FeedbackRecordActivity.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

//	// 实际操作应该是从数据库中读取历史反馈记录
//	@SuppressLint("SimpleDateFormat")
//	private void addListItem() {
//		ListItem item = new ListItem();
//		item.mContent = getResources().getString(R.string.mock_record_first);
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//		item.mDateTime = sdf.format(new Date());
//		mAdapter.getItems().add(item);
//		
//		ListItem item2 = new ListItem();
//		item2.mContent = getResources().getString(R.string.mock_record_second);
//		item2.mDateTime = sdf.format(new Date());
//		mAdapter.getItems().add(item2);
//		mAdapter.notifyDataSetChanged();
//	}


	private void initAdapter() {
		if(mAdapter==null){
			mAdapter = new ListAdapter((ArrayList<Lyzx>) list, FeedbackRecordActivity.this);
			mListView.setAdapter(mAdapter);
		}else{
			mAdapter.notifyDataSetChanged();
		}
	}
	
	class queryQues extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String result= HttpUtils.sendPostMethod(params[0], "utf-8");
			return result;
		}

		@SuppressLint("ShowToast")
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result.equals("fail")){
				Toast.makeText(FeedbackRecordActivity.this, "查询失败", 1).show();
			}else{
				list=getJsonString(result);
				if(list.size()!=0){
					initAdapter();
				}
			}
		}
	}

	private List<Lyzx> getJsonString(String result){
		List<Lyzx> temp =new ArrayList<Lyzx>();
		Lyzx l=null;
		try {
			JSONArray array=new JSONArray(result);
			for(int i=0;i<array.length();i++){
				JSONObject object=array.getJSONObject(i);
				l=new Lyzx(
						object.getString("wtms")
						,object.getString("datetime"));
				temp.add(l);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return temp;
		
	}
}
